import axios from 'axios'

// 创建axios实例
const service = axios.create({
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  },
  timeout: 3000,
  timeoutErrorMessage: '请求超时，请稍后再试'
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    const token = 'token'
    config.token = token
    return config
  },
  error => {
    error.data = {}
    error.data.msg = '请求异常，请联系管理员！'
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    const status = response.status
    let msg = ''
    if (status < 200 || status >= 300) {
      // 处理http错误，弹窗显示
      msg = '请求异常，请联系管理员！'
      return Promise.reject(new Error(msg))
    } else {
      return response.data
    }
  },
  error => {
    return Promise.jectct(error)
  }
)

export default service

